Page({
  data:{
    class:{},
    currentTab: 0,
    teacher:{},
    videos:[] ,
    comments:{},
    load:4,
    user:{},
    showModalStatus: false,
    text:'',
    vurl:''
  },
  onLoad: function (param){
    let _this=this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          clientHeight: res.windowHeight
        });
      }
    });
    _this.setData({
      user: wx.getStorageSync('user')
    })
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    })
    //class
    wx.request({
      url: `https://www.azhouaa.cn/wxs/User/getCourse?id=${param.cid}`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if(res.data.code == 200){
          _this.setData({
            class: res.data.msg
          })
        }
      },
      complete: function () {
        _this.setData({
          load: _this.data.load-1
        })
        if (_this.data.load == 0) wx.hideToast();
      }
    })
    //teacher
    wx.request({
      url: `https://www.azhouaa.cn/wxs/User/getTeacher?id=${param.tid}`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        _this.setData({
          teacher: res.data.msg
        })
      },
      complete: function () {
        _this.setData({
          load: _this.data.load - 1
        })
        if (_this.data.load == 0) wx.hideToast();
      }
    })
    //videos
    wx.request({
      url: `https://www.azhouaa.cn/wxs/User/getVideos?classid=${param.cid}`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        if(res.data.code == 200) {
          _this.setData({
            videos: res.data.msg,
            vurl: res.data.msg[0].videourl
          })
        }
      },
      complete:function(){
        _this.setData({
          load: _this.data.load - 1
        })
        if (_this.data.load == 0) wx.hideToast();
      }
    })
    //comments
    wx.request({
      url: `https://www.azhouaa.cn/wxs/User/getComment?superid=${param.cid}&type=0`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200 && res.data.msg != 'undefined'){
          _this.setData({
            comments: _this.strToTime(res.data.msg)
          })
        }
      },
      complete: function () {
        _this.setData({
          load: _this.data.load - 1
        })
        if (_this.data.load == 0) wx.hideToast();
      }
    })
  },
  bindChange: function (e) {
    let that = this;
    that.setData({ currentTab: e.detail.current });
  },
  /*** 点击tab切换***/
  swichNav: function (e) {
    let that = this,
        current = e.target.dataset.current;
    that.setData({
      currentTab: current
    });
  },
  strToTime:function(comments){
    let _cs = comments;
    for(let c of _cs){
      c.time = new Date(c.time).toLocaleString();
    }
    return _cs
  },
  videoErrorCallback: function (e) {
    console.log('视频错误信息:' + e.detail.errMsg);
    wx.showModal({
      title: '提示',
      content: '视频不存在或服务器错误',
      showCancel:false
    })
  },
  join:function(e){
    let _this = this,
        user = _this.data.user,
        courseid = _this.data.class.id;
    console.log('加入学习',user,courseid)
    wx.request({
      url: `https://www.azhouaa.cn/wxs/User/addStudy?userid=${user.id}&courseid=${courseid}`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        if(res.data.code == 200){
          wx.showToast({
            title: '加入成功',
          })
        }else{
          wx.showModal({
            title: '',
            content: '您已经学习过该课程了',
            showCancel: false,
          })
        }
        
      },
      fail:function(){
        wx.showModal({
          title: '',
          content: '服务器出错',
          showCancel: false,
        })
      }
    })
  },
  addComment:function(){
    this.powerDrawer(null);
  },
  powerDrawer: function (e) {
    let _this = this;
    if (e == null) {
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 150000
      })
      let text = _this.data.text;
      if (text != ''){
        wx.request({
          url: 'https://www.azhouaa.cn/wxs/User/addComment',
          method: 'POST',
          data: {
            "superid": _this.data.class.id,
            "reviewer": _this.data.user.id,
            "text": text,
            "type": 0
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res)
            wx.hideToast();
            _this.util('close');
            if (res.data.code == 200) {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 1500
              });
              let arr = _this.data.comments,
                _comm = res.data.msg;
              _comm['setUser'] = _this.data.user;
              arr.push(_comm);
              _this.setData({
                comments: arr
              })
            } else {
              wx.showModal({
                title: '警告',
                content: '服务器繁忙',
                showCancel: false,
              })
            }
          },
          complete: function () { }
        })
      }else{
        wx.hideToast();
        wx.showModal({
          title: '警告',
          content: '请输入内容',
          showCancel: false,
        })
      }
    } else {
      this.util(e.currentTarget.dataset.statu)
    }

  },
  util: function (currentStatu) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();
    this.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.opacity(1).rotateX(0).step();
      this.setData({
        animationData: animation
      })
      //关闭    
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示    
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
  textInput: function (e) {
    this.setData({
      text: e.detail.value
    })
  },
  setV:function(e){
    this.setData({
      vurl:this.data.videos[e.target.dataset.index]['videourl']
    })
  }

})